<?php
class Realestate_unit_model extends Model{

	var $_datagrid_name = 'dt1';	
	
    function Realestate_unit_model(){
        parent::Model();       
    }
   function getdatasource(){ 
    	$page = $this->input->post($this->_datagrid_name.'_hidden_page')?$this->input->post($this->_datagrid_name.'_hidden_page'):1;    	
    	$limit = $this->config->item('record_limit');   	
    	$count = $this->db->count_all('realestate_unit');    	
    	$pages = intval($count/$limit) + ($count%$limit?1:0);
    	$page = $page>$pages?1:$page;
    	$this->db->limit($limit,($page-1)*$limit);
    	$this->db->select('id,sortorder');
    	$this->db->order_by('sortorder,id');
    	$query = $this->db->get('realestate_unit');
    	$dts = $query->result_rowlist();
    	for($i=0;$i<$query->num_rows;$i++){
    		$dts[$i][2]=$dts[$i][1];
    		$dts[$i][1]=get_content('realestate_unit',$dts[$i][0])->name;
    	}
    	$datagrid->id = $this->_datagrid_name;
    	$datagrid->page = $page;
    	$datagrid->pages = $pages;
    	$datagrid->datasource = $dts;    	
    	return $datagrid;    	
    }
	function delete(){
    	if($this->input->post($this->_datagrid_name.'_hidden_value')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_value');
    		delete_content('realestate_unit',$id);
    		$this->db->delete('realestate_unit',array('id'=>$id));
    	}
    	else if($this->input->post($this->_datagrid_name.'_chk_select')){
    		$id = $this->input->post($this->_datagrid_name.'_chk_select');
    		for($i=0;$i<count($id);$i++){
    			delete_content('realestate_unit',$id[$i]);
	    		$this->db->delete('realestate_unit',array('id'=>$id[$i]));
    		}
    	}
    }
    function addnew(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$this->db->insert('realestate_unit',array('sortorder'=>0));
    		set_content('realestate_unit',$this->db->insert_id(),array('name'=>$this->input->post('txt_categoryname')));
    		redirect('list_unit','location');
    	}
    }
    function update(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		set_content('realestate_unit',$this->input->post('hidden_id'),array('name'=>$this->input->post('txt_categoryname')));
    		redirect('list_unit','location');
    	}
    }
    function getdetail($id){
    	$query = $this->db->get_where('realestate_unit',array('id'=>$id));
    	if($query->num_rows){
    		$detail->id = $id;
	    	$detail->categoryname = get_content('realestate_unit',$id)->name;
	    	return $detail;
    	}
    	else return FALSE;
    }
    function save(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_id');
    		$sortorder = $this->input->post($this->_datagrid_name.'_column_2');
    		for($i=0;$i<count($id);$i++){
    			$this->db->where('id',$id[$i]);
    			$this->db->update('realestate_unit',array('sortorder'=>$sortorder[$i]));
    		}    		
    	}
    }
}

/* End of file yourprofile_model.php */
/* Location: ./system/application/admin/controllers/yourprofile_model.php */